package com.ruoyi.mx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mx.domain.MxGoodsSpu;
import com.ruoyi.mx.service.IMxGoodsSpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 美秀商品Controller
 *
 * @author ruoyi
 * @date 2021-10-28
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mx/goodsSpu" )
public class MxGoodsSpuController extends BaseController {

    private final IMxGoodsSpuService iMxGoodsSpuService;

    /**
     * 查询美秀商品列表
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:list')")
    @GetMapping("/list")
    public TableDataInfo list(MxGoodsSpu mxGoodsSpu)
    {
        startPage();
        LambdaQueryWrapper<MxGoodsSpu> lqw = new LambdaQueryWrapper<MxGoodsSpu>();
        if (StringUtils.isNotBlank(mxGoodsSpu.getSpuCode())){
            lqw.eq(MxGoodsSpu::getSpuCode ,mxGoodsSpu.getSpuCode());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getName())){
            lqw.like(MxGoodsSpu::getName ,mxGoodsSpu.getName());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getSellPoint())){
            lqw.eq(MxGoodsSpu::getSellPoint ,mxGoodsSpu.getSellPoint());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getDescription())){
            lqw.eq(MxGoodsSpu::getDescription ,mxGoodsSpu.getDescription());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getCategoryFirst())){
            lqw.eq(MxGoodsSpu::getCategoryFirst ,mxGoodsSpu.getCategoryFirst());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getCategorySecond())){
            lqw.eq(MxGoodsSpu::getCategorySecond ,mxGoodsSpu.getCategorySecond());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getPicUrls())){
            lqw.eq(MxGoodsSpu::getPicUrls ,mxGoodsSpu.getPicUrls());
        }
        if (StringUtils.isNotBlank(mxGoodsSpu.getShelf())){
            lqw.eq(MxGoodsSpu::getShelf ,mxGoodsSpu.getShelf());
        }
        if (mxGoodsSpu.getSort() != null){
            lqw.eq(MxGoodsSpu::getSort ,mxGoodsSpu.getSort());
        }
        if (mxGoodsSpu.getSalesPrice() != null){
            lqw.eq(MxGoodsSpu::getSalesPrice ,mxGoodsSpu.getSalesPrice());
        }
        if (mxGoodsSpu.getMarketPrice() != null){
            lqw.eq(MxGoodsSpu::getMarketPrice ,mxGoodsSpu.getMarketPrice());
        }
        if (mxGoodsSpu.getCostPrice() != null){
            lqw.eq(MxGoodsSpu::getCostPrice ,mxGoodsSpu.getCostPrice());
        }
        if (mxGoodsSpu.getStock() != null){
            lqw.eq(MxGoodsSpu::getStock ,mxGoodsSpu.getStock());
        }
        if (mxGoodsSpu.getSaleNum() != null){
            lqw.eq(MxGoodsSpu::getSaleNum ,mxGoodsSpu.getSaleNum());
        }
        List<MxGoodsSpu> list = iMxGoodsSpuService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出美秀商品列表
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:export')" )
    @Log(title = "美秀商品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public R export(MxGoodsSpu mxGoodsSpu) {
        LambdaQueryWrapper<MxGoodsSpu> lqw = new LambdaQueryWrapper<MxGoodsSpu>(mxGoodsSpu);
        List<MxGoodsSpu> list = iMxGoodsSpuService.list(lqw);
        ExcelUtil<MxGoodsSpu> util = new ExcelUtil<MxGoodsSpu>(MxGoodsSpu. class);
        return util.exportExcel(list, "goodsSpu" );
    }

    /**
     * 获取美秀商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:query')" )
    @GetMapping(value = "/{id}" )
    public R getInfo(@PathVariable("id" ) String id) {
        return R.success(iMxGoodsSpuService.getById(id));
    }

    /**
     * 新增美秀商品
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:add')" )
    @Log(title = "美秀商品" , businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody MxGoodsSpu mxGoodsSpu) {
        return toAjax(iMxGoodsSpuService.save(mxGoodsSpu) ? 1 : 0);
    }

    /**
     * 修改美秀商品
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:edit')" )
    @Log(title = "美秀商品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody MxGoodsSpu mxGoodsSpu) {
        return toAjax(iMxGoodsSpuService.updateById(mxGoodsSpu) ? 1 : 0);
    }

    /**
     * 删除美秀商品
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsSpu:remove')" )
    @Log(title = "美秀商品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public R remove(@PathVariable String[] ids) {
        return toAjax(iMxGoodsSpuService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
